package com.qf.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.qf.ssm.result.ResponseResult;
import com.qf.ssm.service.CustomerBlackListService;
import com.qf.ssm.vo.CustomerSelectVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author chengqiang
 * @Create 2024-01-04-11:46
 * @Description: 拉黑和删除客户
 */
@RestController
@RequestMapping("/customer/black")
@Api(tags = "客户黑名单接口")
public class CustomerBlackListController {

    @Autowired
    private CustomerBlackListService customerBlackListService;

    @PostMapping("/save")
    @ResponseBody
    @ApiOperation("加入黑名单")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "客户id")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = String.class), @ApiResponse(code = 40000, message = "未知错误")})
    // 拉黑客户
    public ResponseResult<String> deleteCustomer(@RequestParam("id") Integer id){
        System.out.println(id);
        Integer integer = customerBlackListService.saveCustomerBlackList(id);

        if(integer>0){
            return ResponseResult.success(integer+"名客户被拉黑成功");
        }else   if(integer==0){
            return ResponseResult.success(integer+"名客户已被拉黑");
        }
        return ResponseResult.error();

    }
    @DeleteMapping("/delete")
    @ResponseBody
    @ApiOperation("恢复黑名单")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "客户id")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = String.class), @ApiResponse(code = 40000, message = "未知错误")})
    // 拉白客户
    public ResponseResult<String> undeleteCustomer(@RequestParam("id") Integer id){
        Integer integer = customerBlackListService.removeCustomerBlackList(id);
        if(integer>0){
            return ResponseResult.success(integer+"名客户被拉白成功");
        }if(integer==0){
            return ResponseResult.success(integer+"名客户已被拉白");
        }
        return ResponseResult.error();
    }
    @DeleteMapping("/completely")
    @ResponseBody
    @ApiOperation("永久删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "客户id")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = String.class), @ApiResponse(code = 40000, message = "未知错误")})
    // 永久删除客户
    public ResponseResult<Integer> deleteCompletelyCustomer(@RequestParam("id") Integer id){
        // 查询该客户还有多少订单未完成
        Integer orderNum = customerBlackListService.queryCustomerByIdOrder(id);


        if(orderNum>0){
            return ResponseResult.success(-1);
        }

        return ResponseResult.success( customerBlackListService.customerDeleteCompletely(id));

    }
    @GetMapping(value = {"/list/{pageNum}/{pageSize}","/list"})
    @ResponseBody
    @ApiOperation("查询黑名单")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "页码"),@ApiImplicitParam(name = "pageSize", value = "条数")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = CustomerSelectVO.class), @ApiResponse(code = 40000, message = "未知错误")})
    public ResponseResult<PageInfo<CustomerSelectVO>> listBlackList(@PathVariable(required = false) Integer pageNum, @PathVariable(required = false) Integer pageSize){
        if(pageNum==null){
            pageNum=1;
        }
        if(pageSize==null){
            pageSize=4;
        }
        PageInfo<CustomerSelectVO> customerSelectVOPageInfo = customerBlackListService.queryCustomerBlackListMapper(pageNum, pageSize);
        return ResponseResult.success(customerSelectVOPageInfo);
    }




}
